<template>
    <div class="main">
        <div class="bindingwechat" style="margin-top: 12px;">
            <div class="bjcl_fff wh_100" style="margin-bottom: 30px;">
                <Labelcellswx :labelcopy="'微信账号'" :placeholder="'绑定微信账号'" :showrightico="true" :selectvalue="selectvalue" :cbfn="'bindingtirgget'" :showplaceholder="showplaceholder" @bindingtirgget="bindingWx"></Labelcellswx>
            </div>
            <Confimbtn :copy="'保 存'" :disablebtn="disablebtn" :cbfn="cbfn" @confimtrigger="bindSubimt"></Confimbtn>
        </div>
    </div>
    <!-- 组件的模板 -->
        
</template>

<script>
    /**
     *  引入工具类库 或 Native API
     *  example : import widget from 'gapi/widget'
     */

    import * as util from 'js/util.js'
    import widget from 'gapi/widget'
    import commonPage from 'clientApi/commonPage.js'
    import cache from 'gapi/cache.js'
    import safety from 'gapi/safety.js'
    import * as common  from 'js/common.js'
    import userCenterApi from 'clientApi/userCenterApi'
    // import ApiClientUi from 'clientApi/ApiClientUi.js'
    
    /**
     *  引入组件 以及 组件接口(Action)
     *  example : import Somecomponents from 'components/Somecomponents/Somecomponents'
     */

    import Labelcellswx from 'components/Labelcellswx/Labelcellswx'
    import Confimbtn from 'components/Confimbtn/Confimbtn'
    import clientUtil from 'clientApi/util'

    /**
     *  组件实例对象
     */
    export default {
        components: {
            Labelcellswx,
            Confimbtn
        },
        data: function(){
            return{
                disablebtn:true,
                cbfn:'confimtrigger',
                bindType:'wechat',
                selectvalue:'',
                openid:'',
                nickname:'',
                showplaceholder:true,
            }
        },
        methods: {
            /**
             * 初始化
             * @method init
             * @public
             * @return {Null} void
             */
            init (){
                // cache.getCache('WebViewID',(data)=>{
                //     this.WebViewID = data;
                //     commonPage.updateTitleBar({
                //         backAction: 'gotoback',
                //         leftAction: 'gotoback',
                //     })
                //     window.gotoback = () => {
                //         if(this.WebViewID){
                //             commonPage.goBack(1,{},this.WebViewID);
                //         }
                //     }
                // })
            },
            //保存
            bindSubimt(){
               this.disablebtn = true;                
               commonPage.loadPageStatus('loading')                
               util.ajax({
                   url:'/wallet/bind',
                   type:'POST',
                   data:{
                       account: this.nickname,
                       account_type: 2,
                       account_plus: this.openid,
                       name: '',
                   },
                   timeout:10000,
                   success:(data) => {
                       commonPage.loadPageStatus('loadSuccess')
                       this.disablebtn = false;
                       if(data.errno == 0){
                           widget.toast('绑定成功')
                           let encodeStr = encodeURIComponent(`last_pageUrl=2`)
                           location.href = `dmsaas://ApplyforWithdrawal?urdData=${encodeStr}`
                       }else{
                           widget.toast(data.errmsg)
                       }
                   },
                   error:function(status){
                       commonPage.loadPageStatus('loadSuccess')
                       this.disablebtn = false;
                       widget.toast('服务器异常')
                   }
               })
            },
            bindingWx(){
                // if(clientUtil.OS() != 'android'){
                //   userCenterApi.weixinIsInstall(data => {
                //       if(data.status != 1){
                //          widget.toast('请先安装微信')                       
                //       }

                //     })
                // }
                let that = this
                userCenterApi.getWxInfo(data =>{   
                    if(data.status == 1){         
                        that.openid = data.open_id
                        that.nickname = data.nick_name
                        that.selectvalue = data.nick_name
                        that.showplaceholder = false
                        that.disablebtn = false;
                    }   

               })
            }
        },
        created(){

            this.init();
            commonPage.updateTitleBar({
                backAction: 'goBack',
                leftAction: 'goBack'
            })
            window.goBack = () => {
                // location.href = "dmsaas://WithdrawalWay"
                cache.getMemoryCache('yyWithdrayalwayWebViewID',function(data){
                    if(data){
                        commonPage.goBack(1, {}, data.webViewID)
                    }
                })
            }
            
        }
        
    }

    
</script>

<!-- CSS 样式 -->
<!-- <style src="static/css/global.css"></style> -->
<style src="../../static/css/base.css"></style>



